package com.ruoyi.personnel.oa.attendance.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.personnel.management.staff.domain.Staff;
import com.ruoyi.personnel.oa.attendance.domain.Attendance;
import com.ruoyi.personnel.oa.attendance.domain.AttendanceCond;
import com.ruoyi.personnel.oa.attendance.domain.StaffAttendance;
import com.ruoyi.personnel.oa.attendance.service.IAttendanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 视图层
 * 考勤
 */
@RestController
@RequestMapping("/oa/attendance")
@Api(value = "我的考勤模块", tags = "我的考勤接口")
public class AttendanceController extends BaseController {
    @Autowired
    private IAttendanceService attendanceService;

    /**
     * 获取考勤列表
     * @return
     */
    @ApiOperation(value = "获取考勤列表",notes = "查询所有考勤信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody AttendanceCond cond) {
        startPage();
        List<Attendance> attendanceDtos = attendanceService.selectAttendanceList(cond);
        return getDataTable(attendanceDtos);
    }

    /**
     * 获取员工打卡
     * @return
     */
    @ApiOperation(value = "获取员工打卡",notes = "获取员工打卡")
    @PostMapping("/selectStaffAttendanceList")
    public TableDataInfo selectStaffAttendanceList(@RequestBody Staff staff) {
        startPage();
        List<StaffAttendance> staffAttendances = attendanceService.selectStaffAttendanceList(staff);
        return getDataTable(staffAttendances);
    }

    /**
     * 新增考勤
     *
     * @param attendance
     * @return
     */
    @ApiOperation(value = "新增考勤记录", notes = "新增考勤信息")
    @ApiImplicitParam(name = "attendance", value = "新增考勤", dataType = "Attendance")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Attendance attendance) {
        if (attendanceService.selectAttendanceById(attendance.getStaffId(), attendance.getClockTime())) {
            return AjaxResult.error("今天已经打卡，无法重复添加");
        }
        return toAjax(attendanceService.insertAttendance(attendance));
    }

    /**
     * 删除考勤记录
     * 仅修改删除标志位
     *
     * @param attendance
     * @return
     */
    @ApiOperation(value = "删除考勤", notes = "删除考勤,仅删除标志位")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Attendance attendance) {
        return toAjax(attendanceService.updateAttendanceDeleteFlag(attendance));
    }
}
